package kevin.socketdemo;

import kevin.socketDemo.UserData;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketService {

    private  SoketInstance soketInstance;


    public SocketService(SoketInstance soketInstance) {
        this.soketInstance = soketInstance;
    }

    public SoketInstance getSoketInstance() {
        return soketInstance;
    }

    public void setSoketInstance(SoketInstance soketInstance) {
        this.soketInstance = soketInstance;
    }


    public void LinsnteSocket()
    {
        try {
            ServerSocket serverSocket = soketInstance.getSocketInstance();
            Socket socket = serverSocket.accept();
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            Object object = inputStream.readObject();
            System.out.println("服务端收到消息："+object.toString());


            //响应客户端
            ObjectOutputStream objectOutputStream =new ObjectOutputStream(socket.getOutputStream());
            UserData data =new UserData(2,"嗯好的 ");
            objectOutputStream.writeObject(data);
            objectOutputStream.flush();

            inputStream.close();;
            socket.close();
            serverSocket.close();
            objectOutputStream.close();
        }catch (Exception e)
        {
            new RuntimeException("运行异常");
        }finally {

        }


    }
}
